package com.book.oauth.server.client.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.book.common.util.GuavaCacheUtil;
import com.book.common.api.oauth.CacheKey;
import com.book.modules.core.service.impl.BaseServiceImpl;
import com.book.oauth.server.client.entity.Client;
import com.book.oauth.server.client.mapper.ClientMapper;
import com.book.oauth.server.client.service.IClientService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Description：
 * @Author： leo.xiong
 * @CreateDate： 2019/10/12 15:49
 * @Email： leo.xiong@suyun360.com
 * Version：1.0
 */
@Service
@DS(value = "oauth")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ClientServiceImpl extends BaseServiceImpl<ClientMapper, Client> implements IClientService {

    @Override
    public Client getClientByCacheAndDb(String clientId) {
        Client client = GuavaCacheUtil.get(CacheKey.OAUTH_CLIENT + clientId, Client.class);
        if (client != null) {
            return client;
        }
        client = new Client();
        client.setClientId(clientId);
        client = super.getOne(Wrappers.query(client));
        GuavaCacheUtil.put(CacheKey.OAUTH_CLIENT + clientId, client);
        return client;
    }
}
